package eu.dominicum.ft.pd.boundary.ejb;

import eu.dominicum.ft.pd.Person;
import eu.dominicum.ft.pd.boundary.PersonManagement;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * An EJB implementation of the {@link PersonManagement} interface.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class PersonManagementEJB implements PersonManagement {

    /**
     * The entity manager.
     */
    @PersistenceContext(name = "pu_ft")
    private EntityManager entityManager;


    @Override
    public List<Person> findAllPersons() {

        Query namedQuery = entityManager.createNamedQuery("Person.findAll");
        return (List<Person>) namedQuery.getResultList();
    }

    @Override
    public Person findById(final Long personId) {
        return this.entityManager.find(Person.class, personId);
    }
}
